package com.thuanpq60023.easyenglish.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import com.thuanpq60023.easyenglish.R;
import com.thuanpq60023.easyenglish.bus.UnitBus;
import com.thuanpq60023.easyenglish.bus.VocabularyBus;
import com.thuanpq60023.easyenglish.entity.UnitEntity;
import com.thuanpq60023.easyenglish.entity.VocabEntity;
import com.thuanpq60023.easyenglish.view.Header;

public class ListActivity extends Activity {
	private ListView mListView;
	private VocabularyBus mEasyEnglishTBBus;
	private UnitBus mUnitBus;
	private ListActivityAdapter mActivityAdapter;
	private Spinner spnUnit;
	private ProgressDialog mProgressDialog;
	Toast mToast;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_layout);
		intiChild();
		initView();
		initHeader();
	}

	@Override
	protected void onResume() {
		super.onResume();
		loadVocab();
	}

	private void intiChild() {
		mEasyEnglishTBBus = new VocabularyBus(ListActivity.this);
		mUnitBus = new UnitBus(ListActivity.this);
		mProgressDialog = new ProgressDialog(ListActivity.this);
		mProgressDialog.setMessage("Loading...");
		mProgressDialog.setCancelable(false);
	}

	private void initView() {
		mListView = (ListView) findViewById(R.id.list_layout_lst);
		spnUnit = (Spinner) findViewById(R.id.list_layout_spn_unit);

		UnitEntity[] units = mUnitBus.getAllUnit();
		String[] unitnames = new String[units.length];
		for (int i = 0; i < units.length; i++) {
			unitnames[i] = units[i].getName();
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, unitnames);
		adapter.setDropDownViewResource(android.R.layout.select_dialog_item);
		spnUnit.setAdapter(adapter);
		spnUnit.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				loadVocab();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});

		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				VocabEntity temp = (VocabEntity) arg0.getItemAtPosition(arg2);
				createToast(VocabEntity.WORD + ": " + temp.getWord() + "\n"
						+ VocabEntity.MEAN + ": " + temp.getMean() + "\n"
						+ VocabEntity.TYPE + ": " + temp.getType() + "\n"
						+ VocabEntity.UNIT + ": " + temp.getUnit() + "\n"
						+ VocabEntity.EXAMPLE + ": " + temp.getExample() + "\n"
						+ VocabEntity.RIGHT + ": " + temp.getRight() + "\n"
						+ VocabEntity.WRONG + ": " + temp.getWrong() + "\n"
						+ VocabEntity.CREATEDDATE + ": "
						+ temp.getCreatedDate());
			}
		});

		mListView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				VocabEntity entity = (VocabEntity) arg0.getItemAtPosition(arg2);
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), ImportActivity.class);
				intent.putExtra("Word", entity.getWord());
				intent.putExtra("Type", entity.getType());
				Log.e("ThuanPQ", "Word - Type: " + entity.getWord() + " - "
						+ entity.getType());
				startActivity(intent);
				return false;
			}
		});
	}

	private Handler handler = new Handler() {
		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			mProgressDialog.dismiss();
		}
	};

	public void initHeader() {
		Header header = new Header(this);
		header.setTextTitle(getString(R.string.list_layout));
		header.getImvMenu().setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void loadVocab() {
		mProgressDialog.show();
		new SetListviewData().execute(null);
	}

	class SetListviewData extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			try {
				if (spnUnit.getCount() > 0) {
					VocabEntity[] mEntities = mEasyEnglishTBBus
							.getVocabByUnit(spnUnit.getSelectedItem()
									.toString());
					mActivityAdapter = new ListActivityAdapter(mEntities,
							ListActivity.this);
					Log.e("ThuanPQ", Integer.toString(mEntities.length));
				}
			} catch (Exception e) {
				Log.e("ThuanPQ", e.getMessage().toString());
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			mListView.setAdapter(mActivityAdapter);
			handler.sendEmptyMessage(0);
		}
	}

	private void createToast(String text) {
		if (mToast != null) {
			mToast.cancel();
		}
		mToast = Toast.makeText(ListActivity.this, text, Toast.LENGTH_LONG);
		mToast.show();
	}
}
